<template>
  <div class="zone-container">
    <vab-list-notice :content-array="contentArray" />
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form inline :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.zone_name"
              clearable
              placeholder="请输入专区名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
        <el-button :icon="Plus" type="primary" @click="handleAdd">
          添加
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="序号" prop="id" width="55" />
      <el-table-column
        align="center"
        label="专区名称"
        prop="zone_name"
        width="150"
      />
      <el-table-column
        align="center"
        label="宣传标语"
        prop="slogan"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="状态" prop="status" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            active-value="1"
            inactive-value="2"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
        prop="created_at"
        show-overflow-tooltip
      />

      <el-table-column align="center" label="操作" show-overflow-tooltip>
        <template #default="{ row }">
          <el-button text type="primary" @click="handleView(row)">
            会员管理
          </el-button>
          <el-button text type="primary" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button text type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <ZoneDrawer ref="editRef" @fetch-data="fetchData" />
  </div>
</template>
<script>
  import VabListNotice from '@/plugins/VabListNotice'
  import ZoneDrawer from './components/zoneDrawer'
  import { doStatusChange, doDelete, getList } from '@/api/zone'
  import { Delete, Search, Plus } from '@element-plus/icons-vue'
  export default defineComponent({
    name: 'MemberZone',
    components: {
      VabListNotice,
      ZoneDrawer,
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          zone_name: '',
          page: 1,
          pageSize: 10,
        },
        contentArray: [
          '会员分区功能可帮助您快速建立各种主题的会员聚合页面，将会员按照特性标签进行分类集中展示，能够给会员快速的引导入口，增强关注度和流量导入',
        ],
      })

      const handleAdd = () => {
        state['editRef'].showEdit()
      }

      const handleEdit = (row) => {
        state['editRef'].showEdit(row)
      }

      const handleStatusChange = async (row) => {
        try {
          const res = await doStatusChange({ id: row.id, status: row.status })
          if (res.code == 200) {
            $baseMessage(res.msg, 'success', 'vab-hey-message-success')
            fetchData()
          } else {
            $baseMessage(res.msg, 'error', 'vab-hey-message-error')
          }
        } catch (error) {
          $baseMessage(error, 'error', 'vab-hey-message-error')
        }
      }
      const handleDelete = (row) => {
        $baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ id: row.id })
          $baseMessage(msg, 'success', 'vab-hey-message-success')
          await fetchData()
        })
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.page = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.page = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        const imageList = []
        list.forEach((item) => {
          imageList.push(item.url)
        })
        state.imageList = imageList
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleAdd,
        handleEdit,
        handleStatusChange,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Plus,
        Delete,
        Search,
      }
    },
  })
</script>
